import Vue from 'vue'
import VueRouter from 'vue-router'

// 登录页面
const Login = () => import('../views/login/Login')
// 登录后的页面
const Home = () => import('../views/home/Home')
const Book = () => import('../views/memory/components/Book')
const Wirte = () => import('../views/memory/components/Wirte')
const CreateBook = () => import('../views/memory/components/CreateBook')
const Analysis =() => import('../views/analysis')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/page',
    redirect: '/page/home'
  },
  {
    path: '/page/home',
    name: 'home',
    component: Home,
    children: [
      {
        path: '/page/book',
        name: 'Book',
        component: Book
      },
      {
        path: '/page/analysis',
        name: 'Analysis',
        component: Analysis
      },
      {
        path: '/page/createBook',
        name: 'CreateBook',
        component: CreateBook
      },
      {
        path: '/page/wirte/:lid/:docid',
        name: 'Wirte',
        component: Wirte,
        props: true
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})
//  挂载路由导航守卫
router.beforeEach((to, from, next) => {
  const user = JSON.parse(window.sessionStorage.getItem('user'))
  if (to.path.startsWith('/page')) {
    if (user) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
